package br.com.busgps.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.ws.rs.core.MediaType;

import br.com.busgps.mapper.json.Result;

import com.google.gson.Gson;

public class ResultConnection
{
    /**
     * 
     * Obtem os dados do servidor
     * 
     * @param url
     * @return
     * @throws IOException
     */
    public static Result getData(String path) throws IOException
    {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", MediaType.APPLICATION_JSON);

        if (conn.getResponseCode() != 200)
        {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        Reader reader = new InputStreamReader(conn.getInputStream(), "UTF-8");

        Result result = new Gson().fromJson(reader, Result.class);

        conn.disconnect();

        return result;
    }
}
